package de.tum.wi.som.rcpsp.entities;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * The entity Activity illustrates an acitvity in a project schedule. An
 * activity has defined start/end times and has dependencies to other
 * activities.
 * 
 * 
 */
public class Activity {
	private TimePeriod start;
	private TimePeriod end;
	private int neededResources;
	private Set<Activity> priorActivities = new HashSet<Activity>();
	private Set<Activity> followerActivities = new HashSet<Activity>();
	private List<ResourceAssignment> resourceAssignments = new ArrayList<ResourceAssignment>();
	private String name;
	private int lowerBound;
	private int upperBound;
	/**
	 * some Activities cannot be planned into schedule because of their constraints.
	 */
	private boolean planned=true;

	public boolean isPlanned() {
		return planned;
	}

	public void setPlanned(boolean planned) {
		this.planned = planned;
	}

	public Activity(String name, int neededResources, int lowerBound,
			int upperBound) {
		super();
		this.neededResources = neededResources;
		this.name = name;
		this.lowerBound = lowerBound;
		this.upperBound = upperBound;
	}
	
	public Activity(){
		super();
	}

	public TimePeriod getStart() {
		return start;
	}

	public void setStart(TimePeriod start) {
		this.start = start;
	}

	public TimePeriod getEnd() {
		return end;
	}

	public void setEnd(TimePeriod end) {
		this.end = end;
	}

	public int getNeededResources() {
		return neededResources;
	}
	

	public void setNeededResources(int neededResources) {
		this.neededResources = neededResources;
	}

	public boolean addPriorActivity(Activity activity) {
		return priorActivities.add(activity);
	}

	public boolean addFollowerActivity(Activity activity) {
		return followerActivities.add(activity);
	}

	public boolean removePriorActivity(Activity activity) {
		return priorActivities.remove(activity);
	}

	public boolean removeFollowerActivity(Activity activity) {
		return followerActivities.remove(activity);
	}

	public boolean addResourceAssignment(ResourceAssignment resourceAssignment) {
		return resourceAssignments.add(resourceAssignment);
	}

	public ResourceAssignment removeResourceAssignment(TimePeriod time) {
		ResourceAssignment toDelete=null;
		for (ResourceAssignment resAssignment : resourceAssignments) {
			if(resAssignment.getTimePeriod().equals(time)){
				toDelete=resAssignment;
			}
		}
		resourceAssignments.remove(toDelete);
		return toDelete;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getLowerBound() {
		return lowerBound;
	}

	public void setLowerBound(int lowerBound) {
		this.lowerBound = lowerBound;
	}

	public int getUpperBound() {
		return upperBound;
	}

	public void setUpperBound(int upperBound) {
		this.upperBound = upperBound;
	}

	public Set<Activity> getPriorActivities() {
		return priorActivities;
	}

	public Set<Activity> getFollowerActivities() {
		return followerActivities;
	}

	public List<ResourceAssignment> getResourceAssignments() {
		return resourceAssignments;
	}

	@Override
	public String toString() {
		return getName();
	}

	public int getAllocatedResources() {
		int sum = 0;
		for (ResourceAssignment resourceAssignment : getResourceAssignments()) {
			sum += resourceAssignment.getResourceUnits();
		}
		return sum;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		Activity other = (Activity) obj;
		if (name == null) {
			if (other.name != null)
				return false;
		} else if (!name.equals(other.name))
			return false;
		return true;
	}

	public Activity copyWithoutStartEndTime() {
		Activity a = new Activity();
		a.setLowerBound(this.lowerBound);
		a.setUpperBound(this.upperBound);
		a.setName(this.name);
		a.setNeededResources(this.neededResources);
		return a;
	}
	
	

}
